
function now(){return (new Date()).valueOf();}

var jsCSVTester = require('../javascript-csv-tester.js');

var jQueryCSV = require('../jquery.csv-0.7.js');
var rCSV = require('../rcsv.js');

var tests=[
    {
        name:"jQueryCSV",
        code:function(csv){ return jQueryCSV.csv.toArrays(csv); }
    },
    {
        name:"rCSV",
        code:function(csv){ return rCSV.toArrays(csv); }
    }
];

function print(line){console.log(line);}
function round(v){return Math.round(v);}

exports.testData = function(test){

    var iterations = 10;    //change this to 1000 to get reasonable performance statistics; 
                            //10 is just an example

    print("warming up...");

    var testCSV = (jsCSVTester.genCSVWithMetric({rows:1000,cols:20,max:50,esc:0.5}));
    var expected = testCSV.data;
    var result = rCSV.toArrays(testCSV.text);
    test.deepEqual(result, expected, "Generated CSV");
    testCSV = null; result = null; expected = null;

    for(var p = 0.0; p < 0.3; p+=0.1){
        var csv = jsCSVTester.genCSVWithMetric({rows:100,cols:20,max:50,esc:p});
        var out = [round(csv.len),round(csv.esc*100),csv.text.length];
        for(var t = 0; t < tests.length; ++t){
            duration = 0;
            for(var i = 0; i < iterations;++i){
                var start = now();
                tests[t].code(csv.text);
                duration += now()-start;
            }
            duration = round(duration/iterations);
            out.push(tests[t].name);out.push(duration);
        }
        print(out.join(","));
    }
    print("---escaping");
    for(var p = 0.0; p < 1; p+=0.1){
        var csv = jsCSVTester.genCSVWithMetric({rows:1000,cols:10,max:50,esc:p});
        var out = [round(csv.len),round(csv.esc*100),csv.text.length];
        for(var t = 0; t < tests.length; ++t){
            duration = 0;
            for(var i = 0; i < iterations;++i){
                var start = now();
                tests[t].code(csv.text);
                duration += now()-start;
            }
            duration = round(duration/iterations);
            out.push(tests[t].name);out.push(duration);
        }
        print(out.join(","));
    }
    print("---big field");
    for(var p = 1; p < 11; p+=1){
        var csv = jsCSVTester.genCSV(1000/p,["firstName","lastName","favoriteCar",100*p,"favoriteColors","favoriteMonths","favoriteColors"]);
        var out = [100*p,csv.length];
        for(var t = 0; t < tests.length; ++t){
            duration = 0;
            for(var i = 0; i < iterations;++i){
                var start = now();
                tests[t].code(csv);
                duration += now()-start;
            }
            duration = round(duration/iterations);
            out.push(tests[t].name);out.push(duration);
        }
        print(out.join(","));
    }
    test.done();
};


